package ui.server;

import java.util.ArrayList;
import ui.client.Group;
import ui.client.InstantMessage;

/**
 * Abstract class representing the Instant Message protocol used in our client.
 * The run method of this class will handle connect and login in its own thread.
 * 
 * @author hAmmeRoL
 * 
 */
public abstract class AbstractProtocolManager implements Runnable {

	// user name of the account of this protocol
	protected String userID;

	// user password of the account of this protocol
	protected String password;

	// server host name of the protocol server. e.g. talk.google.com
	protected String hostName;

	// port number of the protocol server. e.g. 5222
	protected int port;

	protected ProtocolEventHandler eventHandler;

	public void setEventHandler(ProtocolEventHandler handler) {
		this.eventHandler = handler;
	}

	public ProtocolEventHandler getEventHandler() {
		return eventHandler;
	}

	/**
	 * Connect to the remote server
	 * 
	 * @precondition. none
	 * @postcondition. the connection is established
	 * @modifies. none
	 * 
	 * @throws ProtocolException
	 *             indicating network failure
	 */
	public abstract void connect() throws ProtocolException;

	/**
	 * Login to the protocol server. The login will send an available presence
	 * change to all your clients
	 * 
	 * @precondition. The connection must be established
	 * @postcondition. The user is authenticated
	 * @modifies. none
	 * 
	 * @throws ProtocolException
	 *             Exception indicating failure of connection or login
	 */
	public abstract void login() throws ProtocolException;

	/**
	 * Close the connection to the remote server
	 * 
	 * @precondition. none
	 * @postcondition. The connection to the remote server is lost
	 * @modifies. none
	 * 
	 */
	public abstract void close();

	/**
	 * Send a message using an InstantMessage instance
	 * 
	 * @precondition. the user is logged in and the destination id of the
	 *                message is valid
	 * @postcondition. a message is sent to the client
	 * @modifies. none
	 * 
	 * @param message
	 *            the InstantMessage instance containing the user id,
	 *            destination id and text message
	 * @throws ProtocolException
	 *             Exception indicating failure of sending message
	 */
	public abstract void sendMessage(InstantMessage message)
			throws ProtocolException;

	/**
	 * Get the avatar of the client if available
	 * 
	 * @precondition. the user is logged in and the clientID is valid
	 * @postcondition. get the avatar of the client if available
	 * @modifies. none
	 * 
	 * @param clientID
	 * @return the avatar of the client
	 * @throws ProtocolException
	 *             indicating failure of getting the avatar
	 */
	public abstract byte[] getAvatar(String clientID) throws ProtocolException;

	/**
	 * Get a group list containing all the groups in this manager
	 * 
	 * @precondition. the user is logged in
	 * @postcondition. get all the groups of this protocol manager
	 * @modifies. none
	 * 
	 * @return The ArrayList containing all the groups in this manager
	 */
	public abstract ArrayList<Group> getGroups();

	public String getUserID() {
		return userID;
	}
}
